package com.rongc.client.core.pay.alipay;

import android.app.Activity;
import android.os.AsyncTask;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

public class AlipayHelper {

	public interface onAlipayChecklistener{
		public void checkAccount(boolean isExist);
	}
	
	public interface onAlipayPaylistener{
		public void payResult(String result);
	}


	public static void payByAlipay(final String payInfo, final Activity act, final onAlipayPaylistener listener){
		try {
			new AsyncTask<Void, Void, Map<String,String>>() {
				@Override
				protected Map<String,String> doInBackground(Void... param) {
					Map<String,String> result = null;
					try {
						PayTask alipay = new PayTask(act);
						result = alipay.payV2(payInfo,true);
					} catch (Exception e) {
						e.printStackTrace();
					}
					return result;
				}

				@Override
				protected void onPostExecute(Map<String,String> result) {
					super.onPostExecute(result);
					System.out.println("alipay result:"+result);
					if(listener!=null){
						listener.payResult(result.get("resultStatus"));
					}
				}
			}.execute();
		} catch (Exception e) {
			e.printStackTrace();
			listener.payResult(null);
		}
	}


	/**
	 * 检查付款帐号可用
	 * @param act
	 * @param listener
	 */
	public static void checkAlipay(final Activity act, final onAlipayChecklistener listener){
		new AsyncTask<Void, Void, Boolean>() {
			@Override
			protected Boolean doInBackground(Void... arg0) {
				boolean isExist = false;
				try {
					PayTask payTask = new PayTask(act);
//					isExist = payTask.checkAccountIfExist();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return isExist;
			}
			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				if(listener!=null){
					listener.checkAccount(result);
				}
			}
		}.execute();
	}
	
	
}
